package com.openingedu.mba.screen.adapter;

import java.text.SimpleDateFormat;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lurencun.android.support.widget.CommonAdapter;
import com.openingedu.mba.R;
import com.openingedu.mba.common.CommConsts;
import com.openingedu.mba.persist.DbOrder;

public class OrderAdapter extends CommonAdapter<DbOrder>{

	private Context mContext;
	SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	public OrderAdapter(Context context) {
		super(context);
		mContext = context;
	}

	@Override
	protected View createView(LayoutInflater inflater, final DbOrder data,
			int position, View convertView, ViewGroup parent) {
		LinearLayout orderLayout = (LinearLayout) inflater.inflate(R.layout.item_order, null);
		TextView orderIDView = (TextView) orderLayout.findViewById(R.id.orderID_view);
		TextView orderTypeView = (TextView) orderLayout.findViewById(R.id.orderType_view);
		TextView orderTimeView = (TextView) orderLayout.findViewById(R.id.orderTime_view);
		TextView orderStatusView = (TextView) orderLayout.findViewById(R.id.orderStatus_view);
		TextView paperTitleView = (TextView) orderLayout.findViewById(R.id.paperTitle_view);
		
		String orderId = data.getOrderID();
		orderIDView.setText(orderId);
		if (orderId.startsWith("p")) {
			orderTypeView.setText("购买试题");
			paperTitleView.setVisibility(View.VISIBLE);
			paperTitleView.setText(data.getPaperTitle());
		}
		else if (orderId.startsWith("v")) {
			orderTypeView.setText("购买会员");
			paperTitleView.setVisibility(View.GONE);
		}
		orderTimeView.setText(dataFormat.format(data.getOrderTime()));
		int orderStatus = data.getOrderStatus();
		if (orderStatus == CommConsts.Order.NONE_PAID) {
			orderStatusView.setText("未支付");			
		}
		else if (orderStatus == CommConsts.Order.WAITING_PAID) {
			orderStatusView.setText("等待验证");
		}
		else if (orderStatus == CommConsts.Order.PAID) {
			orderStatusView.setText("已支付");
			orderStatusView.setBackgroundResource(R.drawable.meta_correct_background);
		}
		return orderLayout;
	}
}
